package com.fjnu.controller.shiro;

import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fjnu.controller.common.BaseController;
import com.fjnu.entities.Role;
import com.fjnu.service.IRoleService;

@Controller
public class RoleController extends BaseController{
	private static Logger logger =LogManager.getLogger(RoleController.class);
	@Autowired
	private IRoleService roleService;
	@ResponseBody
	@RequestMapping(value="/addRole")
	public Map<String,Object> addRole(@RequestBody Role role){
		logger.info("添加新角色");
		if(!init(role).get(STATUS).equals(SUCCESS))
			return getData2Result();
		roleService.addRole(role);
		return getData2Result();
	}
	@ResponseBody
	@RequestMapping(value="/delRole")
	public Map<String,Object> delRole(int id){
		logger.info("删除角色");
		if(!init(id).get(STATUS).equals(SUCCESS))
			return getData2Result();
		
		try {
			roleService.delRole(id);
		} catch (Exception e) {
			putError("该角色已被占用，请先删除");
		}
		return getData2Result();
	}
	@ResponseBody
	@RequestMapping(value="/findAllRole")
	public Map<String,Object> findAllRole(){
		logger.info("查询所有角色");
		if(!init().get(STATUS).equals(SUCCESS))
			return getData2Result();
		List<Role> list=roleService.findAllRole();
		put("data",list);
		return getData2Result();
	}
	
}
